﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Level176Letters : MonoBehaviour
{
    public List<TextButton> textButtonList = new List<TextButton>();
    private Dictionary<int, int> activeItemDic = new Dictionary<int, int>();
    private Dictionary<int, string> loveLettersDic = new Dictionary<int, string>();
    private string defaultStr = "a";
    private List<int> loveIndexList = new List<int>();
    public GameLevelItem curGameLevelItem;
    // Start is called before the first frame update
    void Start()
    {
        InitLoveLettersDic();
        InitTextButtons();
    }

    private void InitLoveLettersDic()
    {
        //0-175
        loveLettersDic.Add(27, "l");
        loveLettersDic.Add(60, "i");
        loveLettersDic.Add(105, "g");
        loveLettersDic.Add(126, "h");
        loveLettersDic.Add(166, "t");

        loveIndexList.Add(27);
        loveIndexList.Add(60);
        loveIndexList.Add(105);
        loveIndexList.Add(126);
        loveIndexList.Add(166);
    }

    private void InitTextButtons()
    {
        int count = textButtonList.Count;
        for (int i = 0; i < count; i++)
        {
            string letterStr = defaultStr;
            if (loveLettersDic.ContainsKey(i))
                letterStr = loveLettersDic[i];
            int index = i;
            textButtonList[i].Init(letterStr, () =>
            {
                if (!activeItemDic.ContainsValue(index))
                {
                    AddActiveItem(index);
                }
            });
        }
    }


    private void AddActiveItem(int index)
    {
        int dicCount = activeItemDic.Count;
        if (dicCount > 0)
        {
            for (int i = 1; i < dicCount + 1; i++)
            {
                int oldIndex = activeItemDic[i];
                int nextIndex = oldIndex + 1;
                if (nextIndex >= textButtonList.Count)
                    nextIndex = 0;
                activeItemDic[i] = nextIndex;
            }
        }
        activeItemDic.Add(dicCount + 1, index);
        UpdateLettersColor();
        TriggerResult();
    }

    private void UpdateLettersColor()
    {


        int count = textButtonList.Count;
        for (int i = 0; i < count; i++)
        {
            if (activeItemDic.ContainsValue(i))
                textButtonList[i].text.color = Color.red;
            else
                textButtonList[i].text.color = Color.black;
        }
    }

    private void TriggerResult()
    {
        foreach (KeyValuePair<int, int> kvp in activeItemDic)
        {
            Debug.Log(string.Format("key: {0} value: {1}", kvp.Key, kvp.Value));
        }

        if (activeItemDic.Count >= 5)
        {
            bool result = true;
            for (int i = 0; i < loveIndexList.Count; i++)
            {
                if (!activeItemDic.ContainsValue(loveIndexList[i]))
                    result = false;
            }

            curGameLevelItem.TriggerResult(result);
        }
    }
}
